package com.bh.ly.oa.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import com.bh.ly.oa.GlobalData;
import com.bh.ly.oa.R;
import com.bh.ly.oa.adapter.RelbackOrderListAdapter;
import com.bh.ly.oa.util.AsyncTaskFactory;
import com.bh.ly.oa.util.Config;
import com.bh.ly.oa.util.DateUtil;
import com.bh.ly.oa.util.HttpClent;
import com.bh.ly.oa.util.RequestListType;

public class RelbackOrderViewActivity extends BaseListActiivity{

	private CountDownTimer timer;
    private TextView noticeTextView;
    private GetBackOrderNoticeAsyncTask noticeTask;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.relback_order_view);
        setURLTYPE(Config.listBilReturnfac);
        
        this.noticeTextView = (TextView) findViewById(R.id.notice_msg);
        if (GlobalData.getInstance().isHasBackOrderNotice()) {
            noticeTextView.setText(R.string.notice_unread_back_order_num);
            noticeTextView.setVisibility(View.VISIBLE);
        } else {
            noticeTextView.setVisibility(View.GONE);
        }
        
        timer = new CountDownTimer(Long.MAX_VALUE, 30000) {
            @Override
            public void onFinish() {
            }

            @Override
            public void onTick(long millisUntilFinished) {
                if (noticeTask != null) {
                    noticeTask.detachActivity();
                    noticeTask = null;
                }
                noticeTask = new GetBackOrderNoticeAsyncTask();
                noticeTask.attachActivity(RelbackOrderViewActivity.this);
                AsyncTaskFactory factory = new AsyncTaskFactory();
                factory.build().execute(noticeTask);
            }
            
        };
        timer.start();
        
        initDatas();
    }

    protected void initDatas() {
    	loadingView = findViewById(R.id.loading_view);
		loadingFaildView = (TextView) findViewById(R.id.loading_fiald_view);
    	ImageButton button = (ImageButton) findViewById(R.id.back_button);
        ImageButton rearch_button = (ImageButton) findViewById(R.id.rearch_button);
        button.setVisibility(View.VISIBLE);
        button.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                finish();
            }
        });
        button.setVisibility(View.VISIBLE);
        rearch_button.setVisibility(View.VISIBLE);
        button.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                finish();
            }
        });
        rearch_button.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                Intent intent = new Intent(RelbackOrderViewActivity.this, SearchRelbackOrderViewActivity.class);
                startActivityForResult(intent, RequestListType.SEARCH_REQUEST_CODE_RelbackOrder);
            }
        });
    	
        listView = ((ListView) findViewById(R.id.relback_order_item_list));
        m_footerView = LayoutInflater.from(this).inflate(R.layout.loading, null);
		listView.addFooterView(m_footerView);  
        adapter = new RelbackOrderListAdapter(this);
        ((RelbackOrderListAdapter)adapter).setArray(array);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                Intent intent = new Intent(RelbackOrderViewActivity.this, RelbackOrderDetailViewActivity.class);
                intent.putExtra("json", arg0.getAdapter().getItem(arg2).toString());
                startActivity(intent);
            }
        });
        
        start();
    }
    
    @Override
	protected void start() {
		this.map.put("billno", "");
		this.map.put("startdate", DateUtil.getMondayOFWeek());
		this.map.put("enddate", DateUtil.getCurrentWeekday());
		super.start();
	}

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(data == null) return;
        
        switch (requestCode) {
        case RequestListType.SEARCH_REQUEST_CODE_RelbackOrder:
        	array.clear();
			adapter.notifyDataSetChanged();
        	pageNum = 1;
        	map.put("currentPage", pageNum);
            String startDate = data.getStringExtra("startdate");
            String endDate = data.getStringExtra("enddate");
            this.map.put("billno", data.getStringExtra("billno") == null ? "" : data.getStringExtra("billno"));
            this.map.put("startdate", startDate);
            this.map.put("enddate", endDate);
            super.start();
            break;
        default:
            break;
        }
    }

    @Override
    public void onDetachedFromWindow() {
        if (this.timer != null) {
            this.timer.cancel();
            this.timer = null;
        }
        if (this.noticeTask != null) {
            this.noticeTask.cancel(true);
            this.noticeTask.detachActivity();
            this.noticeTask = null;
        }
        super.onDetachedFromWindow();
    }
    
    private class GetBackOrderNoticeAsyncTask extends AsyncTask<String, Void, Boolean> {
        private Activity activity;

        public void attachActivity(Activity activity) {
            this.activity = activity;
        }

        public void detachActivity() {
            this.activity = null;
        }

        @Override
        protected Boolean doInBackground(String... arg0) {
            return HttpClent.hasBackOrderNoticeToday();
        }

        @Override
        protected void onPostExecute(Boolean code) {
            super.onPostExecute(code);

            GlobalData.getInstance().setHasBackOrderNotice(code == null ? false : code);
            Log.d("GetBackOrderNoticeAsyncTask", "" + code);
            if (this.activity != null) {
                if (code != null && code) {
                    noticeTextView.setText(R.string.notice_unread_back_order_num);
                    noticeTextView.setVisibility(View.VISIBLE);
                } else {
                    noticeTextView.setVisibility(View.GONE);
                }
            }
        }

    }

}
